package com.yl.student.service.impl;

import com.yl.student.AppContent;
import com.yl.student.bean.dto.DataVerifyResponseDto;
import com.yl.student.bean.entity.LoginInfo;
import com.yl.student.bean.entity.ShopInfo;
import com.yl.student.bean.entity.UserInfo;
import com.yl.student.mapper.LoginInfoMapper;
import com.yl.student.service.LoginInfoService;
import com.yl.student.util.UuidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class LoginInfoServiceImpl implements LoginInfoService {
    @Resource
    private LoginInfoMapper loginInfoMapper;
    private DataVerifyResponseDto dataVerifyResponseDto;

    @Override
    public DataVerifyResponseDto verifyLogin(String userId, String pwd,String role) {
        if(userId.equals("") || pwd.equals("") || role.equals("")){
            dataVerifyResponseDto = new DataVerifyResponseDto("未通过","500","账号或密码不能为空","null");
        }else{
            String token = loginInfoMapper.validateLogin(userId,pwd,role);
            if(token==null){
                //表示未从表中查询到数据
                dataVerifyResponseDto = new DataVerifyResponseDto("未通过","500","账号或密码错误","NULL");
            }else{
                dataVerifyResponseDto = new DataVerifyResponseDto("已通过","200","NULL","SUCCESS",token);
            }
        }

        return dataVerifyResponseDto;
    }

    /**
     * 用户注册
     */

    @Override
    public boolean registerUser(LoginInfo loginInfo, UserInfo userInfo) {
        if(loginInfoMapper.getUserIdCount(loginInfo)==1){
            //表示此账户名已经存在
            /**
             * 这里可能存在一个问题：并发，如果多个用户请求，则这个变量会被多次访问，导致变量多次被修改，无法判断是那个用户
             * 的哪次行为进行的修改
             */
            AppContent.isUserUserIdExit = true;
            return false;
        }
        //为loginInfo中生成一个标识该记录的UUID，这个uuid也会作为userInfo表中的userId
        String uuid1 = UuidUtil.generateUUID();
        loginInfo.setId(uuid1);

        //为userInfo表中的id生成一个唯一ID
        String uuid2 = UuidUtil.generateUUID();
        userInfo.setId(uuid2);
        //关联外键
        userInfo.setUserId(uuid1);
        //调用方法，完成数据插入

        int resultLogin = registerUserLoginInfo(loginInfo);
        int resultInfo = registerUserInfo(userInfo);
        if (resultLogin == 1 && resultInfo == 1) {
            return true;
        }else{
            return false;
        }
    }

    @Override
    public int registerUserLoginInfo(LoginInfo loginInfo) {
        return loginInfoMapper.registerUserLoginInfo(loginInfo);
    }

    @Override
    public int registerUserInfo(UserInfo userInfo) {
        return loginInfoMapper.registerUserInfo(userInfo);
    }



    /**
     * 店铺注册
     */
    @Override
    public boolean registerShop(LoginInfo loginInfo, ShopInfo shopInfo) {
        if(loginInfoMapper.getUserIdCount(loginInfo)==1){
            //表示此账户名已经存在
            AppContent.isShopUserIdExit = true;
            return false;
        }
        //为loginInfo中生成一个标识该记录的UUID，这个uuid也会作为userInfo表中的userId
        String uuid1 = UuidUtil.generateUUID();

        loginInfo.setId(uuid1);

        //为userInfo表中的id生成一个唯一ID
        String uuid2 = UuidUtil.generateUUID();
        shopInfo.setId(uuid2);
        //关联外键
        shopInfo.setShopId(uuid1);
        //调用方法，完成数据插入
        int resultLogin = registerShopLoginInfo(loginInfo);
        int resultInfo = registerShopInfo(shopInfo);

        //插入成功
        if (resultLogin == 1 && resultInfo == 1) {
            return true;
        }else{
            return false;
        }
    }

    @Override
    public int registerShopLoginInfo(LoginInfo loginInfo) {
        return loginInfoMapper.registerShopLoginInfo(loginInfo);
    }

    @Override
    public int registerShopInfo(ShopInfo shopInfo) {
        return loginInfoMapper.registerShopInfo(shopInfo);
    }
}
